//
//  StarView.swift
//  zhihuifan
//
//  Created by apple on 2017/9/6.
//  Copyright © 2017年 youyou. All rights reserved.
//

import UIKit

class StarView: UIView {
    var count:String?{
        
        didSet{
            guard let count = count else{
                return
            }
            
        let levels =  stringToInt(str: count)
            for i  in 0 ..< levels {
                let imageView = self.viewWithTag(10 + i)
                imageView?.isHidden = false
            }
        }
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        setupUI()
    }
}
extension StarView{
    fileprivate func setupUI(){
        let itemWith:CGFloat = self.bounds.width / 5
        let backImageView = UIImageView(frame: CGRect(x:0,
                                                      y: 0,
                                                      width: self.bounds.width,
                                                      height: self.bounds.height))
        backImageView.image = UIImage(named:"starbggroud")
        addSubview(backImageView)
        for i  in 0 ..< 5 {
        let imagView = UIImageView(frame: CGRect(x: CGFloat(i) * itemWith, y: 0, width: itemWith, height: 16))
            imagView.image  = UIImage(named:"star_light")
            imagView.isHidden = true
            imagView.tag = 10 + i
            addSubview(imagView)
        }
    }
  fileprivate func stringToInt(str:String)->(Int){
        
        let string = str
        var int: Int?
        if let doubleValue = Double(string) {
            int = Int(doubleValue)
        }
        if int == nil
        {
            return 0
        }
        return int!
    }

}
